﻿using FourWayClimbingDemo.Chess.Advice;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FourWayClimbingDemo.Chess
{
    /// <summary>
    /// AI选手
    /// </summary>
    public class AIPlayer:Player
    {
        public AIPlayer(string name) {
            this._name = name;
            _isAutoWantPlay = true;
        }

        public override (ChessPiece cp, Dir dir)? IWantPlay(ChessBoard chessBoard)
        {
            int socre = 0; 
            ChessPiece cp = null;
            Dir dir = Dir.LEFT;

            foreach (var item in _chessPieces) {
                if (item.IsDead) continue;
                var ca = new CurrentAdvice(chessBoard, this, item);
                var ma = new MoveAdvice(chessBoard, this, item, Dir.LEFT);
                var ra = new ResultAdvice(chessBoard, this, item, Dir.LEFT);
                ca.SetNextAdvice(ma);
                ma.SetNextAdvice(ra);
                var s = ca.Execute();
                if (s > socre) {
                    socre = s;
                    cp = item;
                    dir = Dir.LEFT;
                }

                ca = new CurrentAdvice(chessBoard, this, item);
                ma = new MoveAdvice(chessBoard, this, item, Dir.RIGHT);
                ra = new ResultAdvice(chessBoard, this, item, Dir.RIGHT);
                ca.SetNextAdvice(ma);
                ma.SetNextAdvice(ra);
                s = ca.Execute();
                if (s > socre){
                    socre = s;
                    cp = item;
                    dir = Dir.RIGHT;
                }

                ca = new CurrentAdvice(chessBoard, this, item);
                ma = new MoveAdvice(chessBoard, this, item, Dir.TOP);
                ra = new ResultAdvice(chessBoard, this, item, Dir.TOP);
                ca.SetNextAdvice(ma);
                ma.SetNextAdvice(ra);
                s = ca.Execute();
                if (s > socre){
                    socre = s;
                    cp = item;
                    dir = Dir.TOP;
                }

                ca = new CurrentAdvice(chessBoard, this, item);
                ma = new MoveAdvice(chessBoard, this, item, Dir.BOTTOM);
                ra = new ResultAdvice(chessBoard, this, item, Dir.BOTTOM);
                ca.SetNextAdvice(ma);
                ma.SetNextAdvice(ra);
                s = ca.Execute();
                if (s > socre){
                    socre = s;
                    cp = item;
                    dir = Dir.BOTTOM;
                }
            }

            if (cp == null) return null;

            return (cp,dir);
        }

    }
}
